package com.zsummer.design.create.Builder;

/**
 * @author zsummer
 * 2020/3/22 11:13
 * 建造者模式
 */
public class Client {
    public static void main(String[] args) {
        PersonBuilder chineseBuilder = new ChineseBuilder();
        Directory directory = new Directory(chineseBuilder);
        Person chinese = directory.getPerson();
        System.out.println(chinese);
    }
}

/**
 * 产品
 */
class Person {
    private String hair;
    private String eye;

    public String getHair() {
        return hair;
    }

    public void setHair(String hair) {
        this.hair = hair;
    }

    public String getEye() {
        return eye;
    }

    public void setEye(String eye) {
        this.eye = eye;
    }

    @Override
    public String toString() {
        return "Person{" +
                "hair='" + hair + '\'' +
                ", eye='" + eye + '\'' +
                '}';
    }
}

/**
 * 抽象建造者
 */
abstract class PersonBuilder {

    Person p = new Person();

    abstract PersonBuilder buildHair();

    abstract PersonBuilder buildEye();

    Person build() {
        return p;
    }

}

/**
 * 具体建造者
 */
class ChineseBuilder extends PersonBuilder{
    @Override
    PersonBuilder buildHair() {
        p.setHair("black hair");
        return this;
    }

    @Override
    PersonBuilder buildEye() {
        p.setEye("brown eye");
        return this;
    }
}

/**
 * 指挥者
 */
class Directory {
    private PersonBuilder builder;

    public Directory(PersonBuilder builder) {
        this.builder = builder;
    }

    public Person getPerson(){
        builder.buildEye();
        builder.buildHair();
        return builder.build();
    }
}


